#!/bin/bash

# 创建日志目录
sudo mkdir -p /var/log/welfare-backend
sudo chown -R $(whoami):$(whoami) /var/log/welfare-backend

# 配置gunicorn服务
cat <<EOL | sudo tee /etc/systemd/system/welfare-backend.service
[Unit]
Description=Welfare Backend Service
After=network.target redis.service

[Service]
User=$(whoami)
WorkingDirectory=$(pwd)
Environment="PYTHONPATH=$(pwd)"
ExecStart=${VIRTUAL_ENV}/bin/gunicorn -w 4 -b 0.0.0.0:9000 app:app
Environment="PATH=${VIRTUAL_ENV}/bin:$PATH"
Environment="VIRTUAL_ENV=${VIRTUAL_ENV}"

[Install]
WantedBy=multi-user.target
EOL

# 启用服务
sudo systemctl daemon-reload
sudo systemctl enable welfare-backend
sudo systemctl start welfare-backend

echo "部署完成，服务已启动。使用'sudo systemctl status welfare-backend'查看状态"